/*
 * 文件名：XmlSupport.java
 * 描述： 
 * 修改人： wangshi
 * 修改时间：2012-5-16
 */
package com.ws.base.db.support;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.ws.base.common.WSAppLogger;
import com.ws.base.exception.WSException;

/**
 * XmlSupport
 * 
 * @author wangshi
 * @version
 * @since
 */
public final class NamingXmlSupport {
    private WSAppLogger logger = new WSAppLogger(NamingXmlSupport.class);

    private final static String ENDFILENAME_NAMING = ".naming-sql.xml";

    private static Map<String, String> sqlMap = new HashMap<String, String>();

    private final void readCdata(Element element) throws WSException {
        String sqlId = element.getAttribute("id");
        Node node = element.getFirstChild();
        String cdata = null;
        if (node instanceof CharacterData) {
            cdata = ((CharacterData) node).getNextSibling().getNodeValue();
        }
        if (null != cdata) {
            cdata = cdata.replaceAll("[\\s]{2,}", " ").trim();
        }
        if (!sqlMap.containsKey(sqlId)) {
            sqlMap.put(sqlId, cdata);
        } else {
            throw new WSException("There are the same sqlId, sqlId: " + sqlId);
        }
    }

    public final void analyticalXml(File file) throws WSException {
        DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
        InputStream is = null;
        try {
            DocumentBuilder dombuilder = domfac.newDocumentBuilder();
            is = new FileInputStream(file);
            Document document = dombuilder.parse(is);
            NodeList nodeList = document.getElementsByTagName("sql");
            Element element = null;

            for (int i = 0; i < nodeList.getLength(); i++) {
                element = (Element) nodeList.item(i);
                readCdata(element);
            }

        } catch (ParserConfigurationException e) {
            logger.error(e, null);
        } catch (FileNotFoundException e) {
            logger.error(e, null);
        } catch (SAXException e) {
            logger.error(e, null);
        } catch (IOException e) {
            logger.error(e, null);
        } finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    logger.error(e, null);
                }
            }
        }
    }

    public final void findNamingSqlFile(String path) throws WSException {
        File file = new File(path);
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.getName().endsWith(ENDFILENAME_NAMING)) {
                analyticalXml(f);
            }
        }
    }

    public static void main(String[] args) throws WSException {
        String path = "D:\\workspace3.7\\wsDB\\WebRoot\\WEB-INF\\conf";
        NamingXmlSupport support = new NamingXmlSupport();
        support.findNamingSqlFile(path);
    }

    public final static Map<String, String> getSqlMap() {
        return sqlMap;
    }
}
